﻿var SituatieCO = function(container) {
    var self = this;
    var Elements = {};

    this.An = new Date().getFullYear();
    this.Employee = null;
    var data = null;
    var anInitial = null;
    var isLoading = false;
    var loadingTimeout = null;

    var initialize = function() {
        var template = $(".situatieCOTemplate");
        container.html(template.html());

        Elements = {
            anCurent: container.find(".anCurent"),
            dreptCO: container.find(".dreptCO"),
            zileEfectuate: container.find(".zileEfectuate"),
            zilePlanificate: container.find(".zilePlanificate"),
            zileInitiate: container.find(".zileInitiate"),
            zileRamase: container.find(".zileRamase"),
            dreptTotal: container.find(".dreptTotal"),
            explicatieDreptCO: container.find('.explicatieDreptCO')
        };

        goToYear(self.An);

        Elements.anCurent.change(function() {
            self.An = parseInt(Elements.anCurent.val(), 10);
            $(this).parent().find('.customStyleSelectBoxInner').text(self.An);
            self.refresh();
        });

        Elements.zileEfectuate.click(function(e) {
            if (!app.Pages.CereriConcedii.instance) {
                app.Pages.CereriConcedii.instance = new CereriConcediiPage(app.Pages.CereriConcedii.container);
                app.Pages.CereriConcedii.instance.initialize();
            }
            app.changePage(app.Pages.CereriConcedii);

            app.Pages.CereriConcedii.instance.setSearchData(self.Employee.Name);

            app.Pages.CereriConcedii.instance.setFilterData({
                statusConcediu: {
                    Id: 3,
                    Nume: translate('CereriConcediu_Cerere_Finalizate')
                },
                tipConcediu: {
                    Tip: {
                        TCO_ID: app.TMMData.ConcediuOdihna.TCO_ID,
                        Nume: app.TMMData.ConcediuOdihna.NUME,
                        TIP_CC: app.TMMData.ConcediuOdihna.TIP_CC,
                        NeedsReplacementEmployee: app.TMMData.ConcediuOdihna.TM_NEEDS_REPLACEMENT_EMPLOYEE
                    }
                },
                dataStart: new Date(self.An, 0, 1),
                dataSfarsit: new Date(self.An, 11, Time.getDaysInMonth(self.An, 11)),
                marca: self.Employee.Marca,
                id: self.Employee.Id
            }, true);

            return Event.stopEvent(e);
        });

        Elements.zilePlanificate.click(function(e) {
            if (!app.Pages.CereriConcedii.instance) {
                app.Pages.CereriConcedii.instance = new CereriConcediiPage(app.Pages.CereriConcedii.container);
                app.Pages.CereriConcedii.instance.initialize();
            }
            app.changePage(app.Pages.CereriConcedii);

            app.Pages.CereriConcedii.instance.setSearchData(self.Employee.Name);

            app.Pages.CereriConcedii.instance.setFilterData({
                statusConcediu: {
                    Id: 1,
                    Nume: translate('CereriConcediu_Cerere_Aprobate')
                },
                tipConcediu: {
                    Tip: {
                        TCO_ID: app.TMMData.ConcediuOdihna.TCO_ID,
                        Nume: app.TMMData.ConcediuOdihna.NUME,
                        TIP_CC: app.TMMData.ConcediuOdihna.TIP_CC,
                        NeedsReplacementEmployee: app.TMMData.ConcediuOdihna.TM_NEEDS_REPLACEMENT_EMPLOYEE
                    }
                },
                dataStart: new Date(self.An, 0, 1),
                dataSfarsit: new Date(self.An, 11, Time.getDaysInMonth(self.An, 11)),
                marca: self.Employee.Marca,
                id: self.Employee.Id
            }, true);

            return Event.stopEvent(e);
        });

        Elements.zileInitiate.click(function(e) {
            if (!app.Pages.CereriConcedii.instance) {
                app.Pages.CereriConcedii.instance = new CereriConcediiPage(app.Pages.CereriConcedii.container);
                app.Pages.CereriConcedii.instance.initialize();
            }
            app.changePage(app.Pages.CereriConcedii);

            app.Pages.CereriConcedii.instance.setSearchData(self.Employee.Name);

            app.Pages.CereriConcedii.instance.setFilterData({
                statusConcediu: {
                    Id: 0,
                    Nume: translate('CereriConcediu_Cerere_Initiate')
                },
                tipConcediu: {
                    Tip: {
                        TCO_ID: app.TMMData.ConcediuOdihna.TCO_ID,
                        Nume: app.TMMData.ConcediuOdihna.NUME,
                        TIP_CC: app.TMMData.ConcediuOdihna.TIP_CC,
                        NeedsReplacementEmployee: app.TMMData.ConcediuOdihna.TM_NEEDS_REPLACEMENT_EMPLOYEE
                    }
                },
                dataStart: new Date(self.An, 0, 1),
                dataSfarsit: new Date(self.An, 11, Time.getDaysInMonth(self.An, 11)),
                marca: self.Employee.Marca,
                id: self.Employee.Id
            }, true);

            return Event.stopEvent(e);
        });

        Elements.zileRamase.click(function(e) {
            if (!app.Pages.Cerere.instance)
                app.Pages.Cerere.instance = new CererePage(app.Pages.Cerere.container);
            app.changePage(app.Pages.Cerere);
            app.Pages.Cerere.instance.setCerere(null, self.Employee.Id);
            app.Pages.Cerere.instance.setTipConcediu({
                Tip: {
                    TCO_ID: app.TMMData.ConcediuOdihna.TCO_ID,
                    Nume: app.TMMData.ConcediuOdihna.NUME,
                    TIP_CC: app.TMMData.ConcediuOdihna.TIP_CC,
                    NeedsReplacementEmployee: app.TMMData.ConcediuOdihna.TM_NEEDS_REPLACEMENT_EMPLOYEE
                }
            });
            app.Pages.Cerere.instance.setModified(false);

            return Event.stopEvent(e);
        });

        Elements.explicatieDreptCO.click(function(e) {
            if (!data)
                return;

            var source = $("#dreptCOTemplate").html();
            var comp = Handlebars.compile(source);
            var text = comp({
                AnCurent: translate('SituatieCO_DreptAnCurent') + " " + data.ZileCODreptAnCurent,
                AnAnterior: translate('SituatieCO_DreptAnAnterior') + " " + data.ZileCODreptAnAnterior
            });

            app.MessagesFactory.showDialog(translate('SituatieCO_DreptCO'), text, [
                {
                    Text: 'OK',
                    Callback: function() {
                        app.MessagesFactory.hideDialog();
                    }
                }
            ]);

            return Event.stopEvent(e);
        });
    };

    var goToYear = function(an) {
        self.An = an;
        Elements.anCurent.val(an).trigger('change');
    };

    this.goToCurrentYear = function() {
        goToYear(anInitial);
    };

    this.refresh = function(idChanged, callback) {
        if (isLoading)
            return;

        isLoading = true;

        if (idChanged) {
            container.find('.COcontainer').hide();
            if (loadingTimeout)
                clearTimeout(loadingTimeout);
            loadingTimeout = setTimeout(function() { container.find('.loadMore').show(); }, 300);

            app.TMMService.TMM.GetDataAngajare(self.Employee.Id, function(response) {
                isLoading = false;

                container.find('.COcontainer').show();
                container.find('.loadMore').hide();
                if (loadingTimeout)
                    clearTimeout(loadingTimeout);


                if (!response || response.Exception) {
                    //TODO
                } else {
                    Elements.anCurent.html("");
                    var year = response.FinalResult.parseDate().getFullYear();
                    var anCurent = new Date().getFullYear();
                    anInitial = anCurent;
                    for (var i = year; i <= (anCurent + 1); i++)
                        Elements.anCurent.append("<option value='" + i + "'>" + i + "</option>");
                    goToYear(anCurent);
                };
            });
        } else {
            if (self.Employee) {
                app.TMMService.TMM.GetSituatieCO(self.An, self.Employee.Id, function(response) {
                    isLoading = false;

                    container.find('.COcontainer').show();
                    container.find('.loadMore').hide();

                    if (!response || response.Exception) {
                        //TODO
                    } else {
                        if (response.FinalResult.An != self.An || response.Ref != self.Employee.Id)
                            return;

                        var response = response.FinalResult;
                        data = response;

                        self.Employee.Marca = response.Marca;

                        Elements.dreptTotal.html(response.ZileCODreptTotal);

                        Elements.zileEfectuate.find('.box_content').html(response.ZileCOEfectuate);
                        Elements.zileEfectuate.find('.box_description').html(translate('SituatieCO_ZileEfectuate'));

                        Elements.zilePlanificate.find('.box_content').html(response.ZileCOPlanificate);
                        Elements.zilePlanificate.find('.box_description').html(translate('SituatieCO_ZilePlanificate'));

                        Elements.zileInitiate.find('.box_content').html(response.ZileCOInitiate);
                        Elements.zileInitiate.find('.box_description').html(translate('SituatieCO_ZileInitiate'));

                        Elements.zileRamase.find('.box_content').html(response.ZileCORamase);
                        Elements.zileRamase.find('.box_description').html(translate('SituatieCO_ZileRamase'));
                    }

                    if (callback)
                        callback();
                });
            } else {
                container.find('.COcontainer').show();
                container.find('.loadMore').hide();
            }
        }
    };

    this.hideContainer = function() {
        container.find('.COcontainer').hide();
    };

    this.setEmployee = function(empl) {
        self.Employee = empl;
        self.refresh(true);
    };
    initialize();
};